package com.ztgf.logistics.service.deppon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.logistics.mapper.deppon.LogisticsBillOrderRemarkSnapshotMapper;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillOrderSnapshotBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillOrderRemarkSnapshotDTO;
import com.ztgf.logistics.models.vo.deppon.LogisticsBillOrderSnapshotVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.logistics.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.logistics.mapper.deppon.LogisticsBillOrderSnapshotMapper;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillOrderSnapshotService;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillOrderSnapshotSaveBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillOrderSnapshotDTO;

import java.sql.Struct;

/**
 * 物流订单快照表控制器
 *
 * @author mxli
 * @Date 2021-09-13 22:15:24
 */
@DubboService
public class LogisticsBillOrderSnapshotServiceImpl extends ServiceImpl<LogisticsBillOrderSnapshotMapper, LogisticsBillOrderSnapshotDTO> implements ILogisticsBillOrderSnapshotService{

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private LogisticsBillOrderRemarkSnapshotMapper logisticsBillOrderRemarkSnapshotMapper;

    @Override
    public boolean add(LogisticsBillOrderSnapshotSaveBO saveBO) {
        LogisticsBillOrderSnapshotDTO dto = new LogisticsBillOrderSnapshotDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public Result<LogisticsBillOrderSnapshotVO> get(Long id) {

        LogisticsBillOrderSnapshotDTO dto = this.selectById(id);
        if(dto == null){
            return new Result<>();
        }
        LogisticsBillOrderSnapshotVO vo = new LogisticsBillOrderSnapshotVO();
        BeanUtils.copyProperties(dto, vo);

        LogisticsBillOrderRemarkSnapshotDTO remarkDTO =  logisticsBillOrderRemarkSnapshotMapper.selectById(id);
        if(remarkDTO != null){
            BeanUtils.copyProperties(remarkDTO, vo);

        }
        return new Result<>(vo);
    }

    @Override
    public Result<Page<LogisticsBillOrderSnapshotDTO>> queryPageList(LogisticsBillOrderSnapshotBO bo) {
        Wrapper<LogisticsBillOrderSnapshotDTO> wrapper = new EntityWrapper<>();
        if(bo.getBillId() != null){
            wrapper.eq("bill_id", bo.getBillId());
        }
        if(StrUtils.isNotEmpty(bo.getMailNo())){
            wrapper.like("mail_no", bo.getMailNo());
        }
        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());
        }
        wrapper.orderBy("logistic_id", false);
        Page<LogisticsBillOrderSnapshotDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = this.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
